package org.ami.onlineschool.ui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.ami.onlineschool.dataAccess.EnrolmentDataAccess;
import org.ami.onlineschool.dataAccess.SchoolDataAccess;
import org.ami.onlineschool.dataAccess.StudentListDataAccess;
import org.ami.onlineschool.model.Course;
import org.ami.onlineschool.model.Student;

import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class UIDefineEnrolment extends AbstractWorkWindow{

	private JFrame frmManageEnrolment;
	private JTextField txtTxtlocation;
	private SchoolDataAccess schoolDataAccess;
	private JList lstCources;
	private EnrolmentDataAccess enrolmentDataAccess;
	private JList lstEnrolledStudents;
	private StudentListDataAccess studentListDataAccess;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		launchWindow(new File("").getAbsolutePath());
	}

	static void launchWindow(final String dataLocation) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UIDefineEnrolment window = new UIDefineEnrolment();
					window.setSchoolDataLocation(dataLocation);
					window.initialize();
					window.frmManageEnrolment.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UIDefineEnrolment() {
		//initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmManageEnrolment = new JFrame();
		frmManageEnrolment.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				try {
					enrolmentDataAccess.saveEnrolmentDataOnDisk();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		frmManageEnrolment.setTitle("Manage Enrolment");
		frmManageEnrolment.setBounds(100, 100, 494, 300);
		
		JPanel panel = new JPanel();
		frmManageEnrolment.getContentPane().add(panel, BorderLayout.NORTH);
		panel.setLayout(new BorderLayout(0, 0));
		
		txtTxtlocation = new JTextField();
		txtTxtlocation.setText(getSchoolDataLocation());
		txtTxtlocation.setEnabled(false);
		panel.add(txtTxtlocation, BorderLayout.CENTER);
		txtTxtlocation.setColumns(10);
		
		JButton btnSave = new JButton("Save");
		btnSave.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					enrolmentDataAccess.saveEnrolmentDataOnDisk();
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				}
			}
		});
		panel.add(btnSave, BorderLayout.EAST);
		
		JPanel panel_1 = new JPanel();
		frmManageEnrolment.getContentPane().add(panel_1, BorderLayout.CENTER);
		panel_1.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_1.add(panel_2);
		panel_2.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_4 = new JPanel();
		panel_2.add(panel_4, BorderLayout.NORTH);
		
		JLabel lblCources = new JLabel("Cources");
		panel_4.add(lblCources);
		
		JScrollPane scrollPane = new JScrollPane();
		panel_2.add(scrollPane, BorderLayout.CENTER);
		
		lstCources = new JList();
		lstCources.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				Course selectedCourse = (Course) lstCources.getSelectedValue();
				DefaultListModel listModel = UIUtilities.getListModel(lstEnrolledStudents);
				listModel.clear();
				if(selectedCourse != null) {
					List<String> enrolledStudents = enrolmentDataAccess.getEnrolmentData().get(selectedCourse.getId());
					if(enrolledStudents != null) {
						UIUtilities.populateModelFromList(listModel, studentListDataAccess.getStudents(enrolledStudents));
					}
				}
			}
		});
		scrollPane.setViewportView(lstCources);
		
		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_5 = new JPanel();
		panel_3.add(panel_5, BorderLayout.NORTH);
		
		JLabel lblEnrolledStudents = new JLabel("Enrolled Students");
		panel_5.add(lblEnrolledStudents);
		
		JButton btnEnroll = new JButton("Enroll");
		btnEnroll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Course selectedCourse = (Course) lstCources.getSelectedValue();
				if(selectedCourse != null) {
					List<String> enrolledStudents = enrolmentDataAccess.getEnrolmentData().get(selectedCourse.getId());
					if(enrolledStudents == null) {
						enrolledStudents = new ArrayList<String>();
						enrolmentDataAccess.getEnrolmentData().put(selectedCourse.getId(), enrolledStudents);
					}
					
					Student studentToEnroll = (Student) JOptionPane.showInputDialog(
			                UIDefineEnrolment.this.frmManageEnrolment,
			                "Select student",
			                "Select student",
			                JOptionPane.PLAIN_MESSAGE,
			                null,
			                studentListDataAccess.getStudents().toArray(new Student[0]),
			                null);
					
					enrolledStudents.add(studentToEnroll.getId());
					UIUtilities.getListModel(lstEnrolledStudents).addElement(studentToEnroll);

				}
				
			}
		});
		panel_5.add(btnEnroll);
		
		JButton btnDrop = new JButton("Drop");
		panel_5.add(btnDrop);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		panel_3.add(scrollPane_1, BorderLayout.CENTER);
		
		lstEnrolledStudents = new JList();
		scrollPane_1.setViewportView(lstEnrolledStudents);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		schoolDataAccess = new SchoolDataAccess(getSchoolDataLocation());
		enrolmentDataAccess = new EnrolmentDataAccess(getSchoolDataLocation());
		studentListDataAccess = new StudentListDataAccess(getSchoolDataLocation());
		loadCourses();
	}

	private void loadCourses() {
		UIUtilities.populateModelFromList(UIUtilities.getListModel(lstCources), schoolDataAccess.getSchool().getCourses());
	}
}
